﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Dcms.Common;

public partial class Manage_Modle_Template_FileEdit : System.Web.UI.Page
{
    protected string WebsitePath = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            MyDataBind();
        }
    }
    protected void MyDataBind()
    {
        WebsitePath = Request.ServerVariables["APPL_PHYSICAL_PATH"];
        WebsitePath = WebsitePath.Replace('\\', '/');
        if (DcmsRequest.ValidaQueryStr("Type"))
        {
            HidAction.Value = Request.QueryString["Type"].ToString();
        }
        else
        {
            HidAction.Value = "Create";
        }
        if (DcmsRequest.ValidaQueryStr("Path"))
        {
            HidPath.Value = WebsitePath + Request.QueryString["Path"];
            HidBackPath.Value = Request.QueryString["Path"];
            if (HidAction.Value == "Edit")
            {
                HidBackPath.Value = HidBackPath.Value.Substring(0,HidBackPath.Value.LastIndexOf('/'));
                string Filecode = "uft-8";
                TxbFileName.Text = HidPath.Value.Substring(HidPath.Value.LastIndexOf('/') + 1, HidPath.Value.Length - HidPath.Value.LastIndexOf('/') - 1);
                TxbFileContent.Text = DcmsFileManagerProcessor.ReadTextFile(HidPath.Value, out Filecode);
                DropEncode.SelectedValue = Filecode;
            }
        }
        else
        {
            HidPath.Value = WebsitePath;
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        string FilePath = "", UrlStr = "";
        if (HidAction.Value == "Edit")
        {
            FilePath = HidPath.Value.Substring(0, HidPath.Value.LastIndexOf('/')) + "/" + TxbFileName.Text;
        }
        else
        {
            FilePath = HidPath.Value;
            FilePath = FilePath+"/" + TxbFileName.Text;
        }
        UrlStr = "FileList.aspx?Path=" + HidBackPath.Value;
        try
        {
            DcmsFileManagerProcessor.SaveTextFile(FilePath, TxbFileContent.Text, DropEncode.SelectedValue);
            Response.Write("<script>top.msg_close_tmp_biyuan('" + UrlStr + "');</script>");
        }
        catch (Exception ex)
        {
            Jscript.Alert("\""+ex.Message+"\"");
            return;
        }
    }
}
